﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.Odbc;
using System.Threading;
using System.IO;
namespace TOPO
{
    public partial class Form1 : Form
    {
        private OdbcConnection MyConnection;
        private List<topoLink> mList;
        private List<topoLink> listData;
        public Form1()
        {
            InitializeComponent();
            mList=new List<topoLink>();
            listData = new List<topoLink>();
            toolStripStatusLabel3.Text = DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss");
            Control.CheckForIllegalCrossThreadCalls = false;//不检查线程安全
        }
        
        public string GB2LT(string write)
        {
            //声明字符集   
            System.Text.Encoding iso8859, gb2312;
            //iso8859   
            iso8859 = System.Text.Encoding.GetEncoding("iso8859-1");
            //国标2312   
            gb2312 = System.Text.Encoding.GetEncoding("gb2312");
            byte[] gb;
            gb = gb2312.GetBytes(write);
            //返回转换后的字符   
            return iso8859.GetString(gb);
        }

        //读出时进行转换   
        public string LT2GB(string read)
        {
            //声明字符集   
            System.Text.Encoding iso8859, gb2312;
            //iso8859   
            iso8859 = System.Text.Encoding.GetEncoding("iso8859-1");
            //国标2312   
            gb2312 = System.Text.Encoding.GetEncoding("gb2312");
            byte[] iso;
            iso = iso8859.GetBytes(read);
            //返回转换后的字符   
            return gb2312.GetString(iso);
        }
        private void button1_Click(object sender, EventArgs e)
        {
            button1.Enabled = false;
            toolStripStatusLabel1.Text = "连接数据库...";
            string MyConString = "Driver={MySQL ODBC 5.1 Driver};" +
              "Server=localhost;" +
              "Database=alldb;" +
              "User=root;" +
              "Password=vislecaina;" +
              "Option=3";
            try
            {
                MyConnection = new OdbcConnection(MyConString);
                MyConnection.Open();
                
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString(),"程序出现异常");
            }
            toolStripStatusLabel1.Text = "读取数据中...";
            button1.Enabled = false;
            Thread ReadDataThread = new Thread(ReadFromDb);
            ReadDataThread.Start();
            
        }
        private void ReadFromDb()
        {
            string query1 = "select toponodeid1, boardid1 ,portname1,toponodeid2, boardid2,portname2 from topolink where boardid1!=0";//查询所有连纤
            OdbcCommand cmd1 = new OdbcCommand(query1, MyConnection);
            OdbcDataReader reader1 = cmd1.ExecuteReader();
            while (reader1.Read())
            {
                topoLink tl=new topoLink();
                tl.sne.neid = reader1[0].ToString();
                tl.sne.boardid = reader1[1].ToString();
                tl.sne.portname = reader1[2].ToString();
                tl.dne.neid = reader1[3].ToString();
                tl.dne.boardid = reader1[4].ToString();
                tl.dne.portname = reader1[5].ToString();
                string query2 = "select n.neid,n.nename,nt.ename from ne n,toponode t,_netype nt where nt.netype=n.netype and n.neid=t.neid and t.toponodeid="+tl.sne.neid;//查询网元名字
                OdbcCommand cmd2 = new OdbcCommand(query2, MyConnection);
                OdbcDataReader reader2 = cmd2.ExecuteReader();
                while (reader2.Read())
                {
                    tl.sne.neid = reader2[0].ToString();
                    tl.sne.nename = LT2GB(reader2[1].ToString());
                    tl.sne.NeType = reader2[2].ToString();
                }
                string query3 =" select n.neid,n.nename,nt.ename from ne n,toponode t,_netype nt where nt.netype=n.netype and n.neid=t.neid and t.toponodeid=" + tl.dne.neid;//查询网元名字
                OdbcCommand cmd3 = new OdbcCommand(query3, MyConnection);
                OdbcDataReader reader3 = cmd3.ExecuteReader();
                while (reader3.Read())
                {
                    tl.dne.neid = reader3[0].ToString();
                    tl.dne.nename = LT2GB(reader3[1].ToString());
                    tl.dne.NeType = reader3[2].ToString();
                }
                string query4 = "select b.boardname,b.boardno,b.boardtype,bp.portlineno from board b,boardport bp" +
                    " where b.boardid=bp.boardid and b.boardid="
                    + tl.sne.boardid + " and  bp.portname=\"" + tl.sne.portname+"\"";
                OdbcCommand cmd4 = new OdbcCommand(query4, MyConnection);
                OdbcDataReader reader4 = cmd4.ExecuteReader();
                while (reader4.Read())
                {
                    tl.sne.boardname = reader4[0].ToString();
                    tl.sne.boardno = Int32.Parse(reader4[1].ToString())-1;
                    tl.sne.boardtype = reader4[2].ToString();
                    tl.sne.portno = Int32.Parse(reader4[3].ToString()) %255;
                }
                string query5 = "select b.boardname,b.boardno,b.boardtype,bp.portlineno from board b,boardport bp" +
                " where b.boardid=bp.boardid and b.boardid="
                + tl.dne.boardid + " and  bp.portname=\"" + tl.dne.portname+"\"";
                OdbcCommand cmd5 = new OdbcCommand(query5, MyConnection);
                OdbcDataReader reader5 = cmd5.ExecuteReader();
                while (reader5.Read())
                {
                    tl.dne.boardname = reader5[0].ToString();
                    tl.dne.boardno = Int32.Parse(reader5[1].ToString()) - 1;
                    tl.dne.boardtype = reader5[2].ToString();
                    tl.dne.portno = Int32.Parse(reader5[3].ToString()) % 255;
                }
                tl.sne.init();
                tl.dne.init();
                mList.Add(tl);
            }
            foreach (topoLink tp in mList)
            {
                 if (tp.sne.netype==neType.IPRAN)
                {
                    listData.Add(tp);
                }
                if (tp.dne.netype==neType.IPRAN)
                {
                    topoLink temp = new topoLink();
                    temp.sne = tp.dne;
                    temp.dne = tp.sne;
                    listData.Add(temp);
                }
               
            }
            listData.Sort(Compare);
            toolStripStatusLabel1.Text = "就绪";       
            button1.Enabled = true;
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            toolStripStatusLabel3.Text = DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss");
        }
        private void putStrToFile(string str)
        {

            FileStream fs = new FileStream(Properties.Settings.Default.logfile, FileMode.OpenOrCreate, FileAccess.Write);

            StreamWriter m_streamWriter = new StreamWriter(fs);

            m_streamWriter.BaseStream.Seek(0, SeekOrigin.End);

            //m_streamWriter.WriteLine("My Service:"+str + "   "+DateTime.Now.ToString() + "\n");
            m_streamWriter.WriteLine(str);
            m_streamWriter.Flush();

            m_streamWriter.Close();

            fs.Close();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            putStrToFile("端口模式,本端网元,本端端口,对端网元,对端端口");
            foreach (topoLink tp in listData)
            {
                if(tp.sne.netype==neType.IPRAN)
                {
                    string outstr="";
                    if(tp.dne.netype==neType.IPRAN)
                        outstr+="L3,";
                    else
                        outstr+="TP,";
                    outstr+=tp.sne.nename+","+tp.sne.portstr+",";
                    outstr+=tp.dne.nename+","+tp.dne.portstr;
                    putStrToFile(outstr);
                }
            }
        }
        private static int Compare(topoLink tp1, topoLink tp2)
        {
            if (tp1.sne.neid==tp2.sne.neid)
            {
                return (tp2.dne.NeType.CompareTo(tp1.dne.NeType));
            }
            return tp1.sne.neid.CompareTo(tp2.sne.neid);
        }
    }
}
